<template>
    <div class="member-data">

        <div class="func-block" @click="addMemberDialog = true">
            <el-button type="primary" plain icon="el-icon-circle-plus-outline">新增成员</el-button>
        </div>

        <div class="func-block" @click="searchMemberDialog = true">
            <el-button type="warning" plain icon="el-icon-search">精准筛选</el-button>
        </div>

        <div class="func-block" @click="queryMemberTableList()">
            <el-button type="primary">清除筛选</el-button>
        </div>

        <el-table :data="memberTableData" style="width: 100%" v-loading="tableLoader">

            <el-table-column label="成员ID" prop="memberId" width="150" align="center">
            </el-table-column>

            <el-table-column label="成员工号" prop="memberNum" width="150" align="center">
            </el-table-column>

            <el-table-column label="成员权限" prop="role" width="150" align="center">
            </el-table-column>

            <el-table-column label="登录IP" prop="loginIp" width="180" align="center">
            </el-table-column>

            <el-table-column label="登录时间" width="200" align="center">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.loginTime | dateFormat("yyyy-mm-dd hh:mm:ss") }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="250" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row.memberId)" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row.memberId)"
                        icon="el-icon-delete">删除</el-button>
                </template>
            </el-table-column>

        </el-table>
        <!-- 每页5条 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="queryMemberTableList" />

        <el-dialog title="新增成员" :visible.sync="addMemberDialog" width="30%" center>
            <div class="add-member-form">
                <el-form :model="addMemberForm" status-icon :rules="addMemberRules" ref="addMemberForm" label-width="100px">
                    <el-form-item label="成员工号" prop="memberNum">
                        <el-input type="text" v-model="addMemberForm.memberNum" placeholder="成员工号[4,7]"></el-input>
                    </el-form-item>
                    <el-form-item label="成员密码" prop="password">
                        <el-input type="password" v-model="addMemberForm.password" placeholder="密码[6,10]"></el-input>
                    </el-form-item>
                    <el-form-item label="成员权限" prop="role">
                        <el-select v-model="addMemberForm.role" placeholder="请选择成员权限">
                            <el-option v-for="item in memberRoles" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addMemberDialog = false">取 消</el-button>
                <el-button type="success" @click="insertMember('addMemberForm')" v-if="!addLoader">添 加</el-button>
                <el-button type="success" :loading="addLoader" v-else>添 加 中...</el-button>
            </span>
        </el-dialog>

        <el-dialog title="筛选查询" :visible.sync="searchMemberDialog" width="40%" center>
            <el-form ref="searchMemberForm" :model="searchMemberForm" label-width="80px">
                <el-form-item label="成员工号">
                    <el-input v-model="searchMemberForm.memberNum"></el-input>
                </el-form-item>
                <el-form-item label="成员权限">
                    <el-select v-model="searchMemberForm.role" placeholder="请选择成员权限">
                        <el-option v-for="item in memberRoles" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="登录时间">
                    <el-col :span="8">
                        <el-date-picker type="date" placeholder="选择起始日期" v-model="searchMemberForm.startDate"
                            style="width: 100%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-col>
                    <el-col style="text-align: center;" :span="1">-</el-col>
                    <el-col :span="8">
                        <el-date-picker placeholder="选择截至日期" v-model="searchMemberForm.endDate" style="width: 100%;"
                            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                    </el-col>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="searchMemberDialog = false">取 消</el-button>
                <el-button type="warning" @click="searchMember()">查 询</el-button>
            </span>
        </el-dialog>

        <el-dialog title="编辑成员" :visible.sync="editMemberDialog" width="30%" center>
            <div class="edit-member-form">
                <el-form :model="editMemberForm" status-icon :rules="editMemberRules" ref="editMemberForm"
                    label-width="100px">
                    <el-form-item label="成员工号" prop="memberNum">
                        <el-input type="text" v-model="editMemberForm.memberNum" placeholder="成员工号[4,7]"></el-input>
                    </el-form-item>
                    <el-form-item label="成员密码" prop="password">
                        <el-input type="password" v-model="editMemberForm.password" placeholder="密码[6,10]"></el-input>
                    </el-form-item>
                    <el-form-item label="成员权限" prop="role">
                        <el-select v-model="editMemberForm.role" placeholder="请选择成员权限">
                            <el-option v-for="item in memberRoles" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editMemberDialog = false">取 消</el-button>
                <el-button type="primary" @click="editMember('editMemberForm')" v-if="!editLoader">修 改</el-button>
                <el-button type="primary" :loading="editLoader" v-else>修 改 中...</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>

import { addMember, queryMemberList, queryEditMemberInfo, editMember, deleteMember } from '@/api/member'
import { Message } from 'element-ui'

export default {
    name: 'sysMember',

    data() {
        return {
            addMemberDialog: false,
            searchMemberDialog: false,
            addMemberForm: {
                memberId: '',
                memberNum: '',
                password: '',
                role: '普通管理员'
            },
            addMemberRules: {
                memberNum: [
                    {
                        required: true,
                        message: "工号不可为空",
                        trigger: "blur",
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "密码不可为空",
                        trigger: "blur",
                    },
                ]
            },
            addLoader: false,

            searchMemberForm: {
                memberNum: '',
                role: '',
                startDate: '',
                endDate: ''
            },

            memberTableData: null,
            tableLoader: false,
            queryParams: {
                pageNum: 1,
                pageSize: 5
            },
            total: 0,

            editMemberDialog: false,
            editMemberForm: {
                memberId: '',
                memberNum: '',
                password: '',
                role: '普通管理员'
            },
            editMemberRules: {
                memberNum: [
                    {
                        required: true,
                        message: "工号不可为空",
                        trigger: "blur",
                    },
                ],
                password: [
                    {
                        required: true,
                        message: "密码不可为空",
                        trigger: "blur",
                    },
                ]
            },
            editLoader: false,
            memberRoles: [{
                value: '超级管理员',
                label: '超级管理员'
            }, {
                value: '普通管理员',
                label: '普通管理员'
            }, {
                value: '客服审核员',
                label: '客服审核员'
            }]
        }
    },

    created() {
        this.tableLoader = true
        this.queryMemberTableList()
    },

    methods: {
        insertMember(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.addLoader = true;
                    addMember(this.addMemberForm).then((res) => {
                        if (res && res.code === 200) {
                            Message.success("添加成功");
                            this.$refs[formName].resetFields();
                            this.addMemberDialog = false
                            this.tableLoader = true
                            this.queryMemberTableList()
                        }
                        this.addLoader = false;
                    });
                } else {
                    this.addLoader = false;
                    return false;
                }
            });
        },

        searchMember() {
            this.searchMemberDialog = false
            this.tableLoader = true
            this.queryMemberTableList()
            this.searchMemberForm = Object.assign({}, {
                memberNum: '',
                role: '',
                startDate: '',
                endDate: ''
            })
        },

        queryMemberTableList() {
            queryMemberList(this.queryParams,
                this.searchMemberForm).then((res) => {
                    this.memberTableData = res.rows
                    this.total = res.total
                    this.tableLoader = false
                })
        },

        handleEdit(id) {
            this.editMemberDialog = true
            queryEditMemberInfo(id).then((res) => {
                if (res.code === 200) {
                    this.editMemberForm = res.data
                }
            })
        },

        handleDelete(id) {
            this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                deleteMember(id).then((res) => {
                    if (res.code === 200) {
                        Message.success("删除成功");
                        this.tableLoader = true
                        this.queryMemberTableList()
                    }
                })
            });
        },

        editMember(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.editLoader = true;
                    editMember(this.editMemberForm).then((res) => {
                        if (res && res.code === 200) {
                            Message.success("编辑成功");
                            this.$refs[formName].resetFields();
                            this.editMemberDialog = false
                            this.tableLoader = true
                            this.queryMemberTableList()
                        }
                        this.editLoader = false;
                    });
                } else {
                    this.editLoader = false;
                    return false;
                }
            });
        }
    },

    filters: {
        dateFormat: function (val, args) {
            if (val) {
                var dt = new Date(val);
                var year = dt.getFullYear();
                var month = ("0" + (dt.getMonth() + 1)).slice(-2);
                var day = ("0" + dt.getDate()).slice(-2);
                if (args.toLowerCase() === "yyyy-mm-dd") {
                    return year + "-" + month + "-" + day;
                } else {
                    var hour = ("0" + dt.getHours()).slice(-2);
                    var min = ("0" + dt.getMinutes()).slice(-2);
                    var sec = ("0" + dt.getSeconds()).slice(-2);
                    return (
                        year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
                    );
                }
            } else {
                return '';
            }
        },
    },
}
</script>

<style lang="scss" scoped>
.func-block {
    margin-bottom: 20px;
    display: inline-block;
    margin-right: 20px;
}
</style>